import Vue from 'vue'
import Router from 'vue-router'
import home from './views/Home.vue'
import login from './views/login.vue'
import LegalPersonPhoto from './views/LegalPersonPhoto.vue'
import auxiliary from './views/auxiliary.vue'
import store from './store'
import { Tools, browser } from '@/lib/tools'
import axios from "axios"

Vue.use(Router)

const router = new Router({
    // base: process.env.BASE_URL,
    routes: [
        {
            path: '/login',
            name: 'login',
            component: login,
        },
        {
            path: '/',
            name: 'home',
            component: home,
        },
        {
            path: '/LegalPersonPhoto',
            name: 'LegalPersonPhoto',
            component: function () {
                return import('./views/LegalPersonPhoto.vue')
            }
        },
        {
            path: '/auxiliary',
            name: 'auxiliary',
            component: function () {
                return import('./views/auxiliary.vue')
            }
        }, {
            path: '/ShopPhoto',
            name: 'ShopPhoto',
            component: function () {
                return import('./views/ShopPhoto.vue')
            }
        }, {
            path: '/companyAddress',
            name: 'companyAddress',
            component: function () {
                return import('./views/companyAddress.vue')
            }
        }, {
            path: '/merchantAccount',
            name: 'merchantAccount',
            component: function () {
                return import('./views/merchantAccount.vue')
            }
        },
        {
            path: '/Authentication',
            name: 'Authentication',
            component: function () {
                return import('./views/Authentication.vue')
            }
        },
        {
            path: '/companyAddress',
            name: 'companyAddress',

            component: function () {
                return import('./views/companyAddress.vue')
            }
        },
        {
            path: '/merchantAccount',
            name: 'merchantAccount',
            component: function () {
                return import('./views/merchantAccount.vue')
            }
        }, {
            path: '/error',
            name: 'error',
            component: function () {
                return import('./views/error.vue')
            }
        }, {
            path: '/goSign',
            name: 'goSign',
            component: function () {
                return import('./views/goSign.vue')
            }
        }, {
            path: '/successfulSigning',
            name: 'successfulSigning',
            component: function () {
                return import('./views/successfulSigning.vue')
            }
        }, {
            path: '/inAudit',
            name: 'inAudit',
            component: function () {
                return import('./views/inAudit.vue')
            }
        }, {
            path: '/auditSuccess',
            name: 'auditSuccess',
            component: function () {
                return import('./views/auditSuccess.vue')
            }
        },{
            path: '/searchBranch',
            name: 'searchBranch',
            component: function () {
                return import('./views/searchBranch.vue')
            }
        },
    ]
})
router.beforeEach((to, from, next) => {
    //刷新缓存
    if (Tools.check.isEmpty(from.name)){
        if (!Tools.check.isEmpty(sessionStorage.state))
            store.replaceState( Tools.convert.base64ToJson(sessionStorage.state) );
        if ( browser.versions.ios ) {
            window.addEventListener('pagehide', () => {
                sessionStorage.setItem('state', Tools.convert.jsonToBase64(store.state))
            })
        } else {
            window.addEventListener('beforeunload', (event) => {
                event = event || window.event;
                event.preventDefault();
                sessionStorage.setItem('state', Tools.convert.jsonToBase64(store.state))
            })
        }
        next()
    }else{
        next()
    }
})


export default router
